package minidb.queryexec;

import java.util.*;

public class RidPairSet extends RidSet
{
	private Set<String> pairSet;
	private String tableNameL;
	private String tableNameR;
	
	public RidPairSet(String tnL,String tnR)
	{
		this.pairSet=new HashSet<String>();
		this.tableNameL=tnL;
		this.tableNameR=tnR;
	}
	
	public void AddPair(String pair)
	{
		this.pairSet.add(pair);
	}
	
	public void AddPair(String tn1,String rid1,String rid2)
	{
		if(tn1.equals(this.tableNameL))
			this.pairSet.add(rid1+"#"+rid2);
		else 
			this.pairSet.add(rid2+"#"+rid1);
	}
	
	
	public void AddPairLR(String rid1,String rid2)
	{
		this.pairSet.add(rid1+"#"+rid2);
	}
	
	public String GetSingle(String pair,String tn)
	{
		int i=pair.indexOf('#');
		if(tn.equals(this.tableNameL))
			return pair.substring(0, i);
		else 
			return pair.substring(i+1);
	}
	
	public static String GetSingleL(String pair)
	{
		int i=pair.indexOf('#');
		return pair.substring(0,i);
	}
	
	public static String GetSingleR(String pair)
	{
		int i=pair.indexOf('#');
		return pair.substring(i+1);
	}	
	
	public RidSingleSet ToSingleSet(String tn)
	{
		RidSingleSet ss=new RidSingleSet(tn);
		
		for(String pair:this.pairSet)
		{
			String single=this.GetSingle(pair, tn);
			ss.AddSingle(single);
		}
		return ss;
	}
	
	public Set<String> GetRidSet()
	{
		return this.pairSet;
	}
	
	public String GetTableNameL()
	{
		return this.tableNameL;
	}
	
	public String GetTableNameR()
	{
		return this.tableNameR;
	}
}



